﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;

namespace The_LarX
{
    public class AnimationHelper
    {
        private float framesWent;
        private bool finished;
        public bool Finished
        {
            get { return finished; }
        }
        private float desacelerating;

        private float framesToDesacelerate = 30;
        public float FramesToDesacelerate
        {
            get { return framesToDesacelerate; }
            set
            {
                framesToDesacelerate = value;
                desacelerating = value;
            }
        }

        public AnimationHelper()
        {
        }

        #region Methods

        public void Reset()
        {
            desacelerating = framesToDesacelerate;
            framesWent = 0;
            finished = false;
        }
        public Vector3 Animate(Vector3 ActualPosition, Vector3 Destiny, float frames)
        {
            framesWent++;
            float Frames = frames - framesWent;
            float desaceleration = 0;
            if (Frames <= framesToDesacelerate)
            {
                desaceleration = desacelerating / frames;
                desacelerating -= framesToDesacelerate / frames;
            }
            if (Frames <= 0)
            {
                finished = true;
                return ActualPosition;
            }
            float X = 0;
            float Y = 0;
            float Z = 0;
            if (Destiny.X > ActualPosition.X)
                X = (Destiny.X - ActualPosition.X) / Frames;
            else
                X = (ActualPosition.X - Destiny.X) / Frames;
            if (Destiny.Y > ActualPosition.Y)
                Y = (Destiny.Y - ActualPosition.Y) / Frames;
            else
                Y = (ActualPosition.Y - Destiny.Y) / Frames;
            if (Destiny.Z > ActualPosition.Z)
                Z = (Destiny.Z - ActualPosition.Z) / Frames;
            else
                Z = (ActualPosition.Z - Destiny.Z) / Frames;

            X += desaceleration;
            Y += desaceleration;
            Z += desaceleration;

            if (ActualPosition.X > Destiny.X)
                ActualPosition.X -= X;
            if (ActualPosition.X < Destiny.X)
                ActualPosition.X += X;
            if (ActualPosition.Y > Destiny.Y)
                ActualPosition.Y -= Y;
            if (ActualPosition.Y < Destiny.Y)
                ActualPosition.Y += Y;
            if (ActualPosition.Z > Destiny.Z)
                ActualPosition.Z -= Z;
            if (ActualPosition.Z < Destiny.Z)
                ActualPosition.Z += Z;

            return ActualPosition;
        }

        #endregion

        #region Static Methods
        public static Rectangle AnimateStatic(Rectangle ActualPosition, Rectangle Destiny, int Frames)
        {
            ActualPosition.X = (int)AnimateStatic(ActualPosition.X, Destiny.X, Frames);
            ActualPosition.Y = (int)AnimateStatic(ActualPosition.Y, Destiny.Y, Frames);
            ActualPosition.Width = (int)AnimateStatic(ActualPosition.Width, Destiny.Width, Frames);
            ActualPosition.Height = (int)AnimateStatic(ActualPosition.Height, Destiny.Height, Frames);
            
            return ActualPosition;
        }


        public static Vector3 AnimateStatic(Vector3 ActualPosition, Vector3 Destiny, int Frames)
        {
            float X = 0;
            float Y = 0;
            float Z = 0;
            if (Destiny.X > ActualPosition.X)
                X = (Destiny.X - ActualPosition.X) / Frames;
            else
                X = (ActualPosition.X - Destiny.X) / Frames;
            if (Destiny.Y > ActualPosition.Y)
                Y = (Destiny.Y - ActualPosition.Y) / Frames;
            else
                Y = (ActualPosition.Y - Destiny.Y) / Frames;
            if (Destiny.Z > ActualPosition.Z)
                Z = (Destiny.Z - ActualPosition.Z) / Frames;
            else
                Z = (ActualPosition.Z - Destiny.Z) / Frames;

            if (ActualPosition.X > Destiny.X)
                ActualPosition.X -= X;
            if (ActualPosition.X < Destiny.X)
                ActualPosition.X += X;
            if (ActualPosition.Y > Destiny.Y)
                ActualPosition.Y -= Y;
            if (ActualPosition.Y < Destiny.Y)
                ActualPosition.Y += Y;
            if (ActualPosition.Z > Destiny.Z)
                ActualPosition.Z -= Z;
            if (ActualPosition.Z < Destiny.Z)
                ActualPosition.Z += Z;

            return ActualPosition;
        }

        public static Vector4 AnimateStatic(Vector4 ActualPosition, Vector4 Destiny, int Frames)
        {
            ActualPosition.X = AnimateStatic(ActualPosition.X, Destiny.X, Frames);
            ActualPosition.Y = AnimateStatic(ActualPosition.Y, Destiny.Z, Frames);
            ActualPosition.Z = AnimateStatic(ActualPosition.Z, Destiny.Y, Frames);
            ActualPosition.W = AnimateStatic(ActualPosition.W, Destiny.W, Frames);
            return ActualPosition;
        }
        public static Color AnimateStatic(Color ActualColor, Color Destiny, int Frames)
        {
            ActualColor.R = (byte)AnimateStatic(ActualColor.R, Destiny.R, Frames);
            ActualColor.G = (byte)AnimateStatic(ActualColor.G, Destiny.G, Frames);
            ActualColor.B = (byte)AnimateStatic(ActualColor.B, Destiny.B, Frames);
            ActualColor.A = (byte)AnimateStatic(ActualColor.A, Destiny.A, Frames);
            return ActualColor;
        }


        public static Vector2 AnimateStatic(Vector2 ActualPosition, Vector2 Destiny, int Frames)
        {
            float X = 0;
            float Y = 0;
            if (Destiny.X > ActualPosition.X)
                X = (Destiny.X - ActualPosition.X) / Frames;
            else
                X = (ActualPosition.X - Destiny.X) / Frames;
            if (Destiny.Y > ActualPosition.Y)
                Y = (Destiny.Y - ActualPosition.Y) / Frames;
            else
                Y = (ActualPosition.Y - Destiny.Y) / Frames;

            if (ActualPosition.X > Destiny.X)
                ActualPosition.X -= X;
            if (ActualPosition.X < Destiny.X)
                ActualPosition.X += X;
            if (ActualPosition.Y > Destiny.Y)
                ActualPosition.Y -= Y;
            if (ActualPosition.Y < Destiny.Y)
                ActualPosition.Y += Y;

            return ActualPosition;
        }
        public static float AnimateStatic(float ActualPosition, float Destiny, int Frames)
        {
            float X = 0;
            if (Destiny > ActualPosition)
                X = (Destiny - ActualPosition) / Frames;
            else
                X = (ActualPosition - Destiny) / Frames;

            if (ActualPosition > Destiny)
                ActualPosition -= X;
            if (ActualPosition < Destiny)
                ActualPosition += X;

            return ActualPosition;
        }
        #endregion
    }
}
